import {
  getOrderRecordListFun,
  cancelRecordFun,
  addItemReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";

export default {
  namespace,
  state: {
    init: false,
    recordList: [], //预约记录
  },
  effects: {
    //预约记录
    *getOrderRecordList({ params }, { call, put }) {
      const response = yield call(getOrderRecordListFun, params);
      let records = [];
      let total = 0;
      if (response.code == 200 && response.data) {
        records = response.data.records;
        total = response.data.total;
      } else {
        message.error(response.msg);
      }
      return {
        success: true,
        data: records,
        total: total,
      };
    },
    //取消预约
    *cancelRecord({ params, callback }, { call, put }) {
      const response = yield call(cancelRecordFun, params);
      if (response.code === 200) {
        callback && callback();
      } else {
        message.warning(response.msg);
      }
    },

    *submitItem({ params, callback }, { call }) {
      let res = null;
      if (params.id) {
        res = yield call(updateItemReq, params);
      } else {
        res = yield call(addItemReq, params);
      }
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res) {
        callback && callback();
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
